

package com.hazelcast.client.impl.spi.impl.listener;

import com.hazelcast.client.impl.spi.ProxyManager;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.DistributedObjectEvent;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.Serial;
import java.util.UUID;

@SuppressFBWarnings("SE_BAD_FIELD")
public final class LazyDistributedObjectEvent extends DistributedObjectEvent {
    @Serial
    private static final long serialVersionUID = 6564764769334087775L;
    private ProxyManager proxyManager;

    /**
     * Constructs a DistributedObject Event.
     *
     * @param eventType         The event type as an enum {@link EventType} integer.
     * @param serviceName       The service name of the DistributedObject.
     * @param objectName        The name of the DistributedObject.
     * @param distributedObject The DistributedObject for the event.
     * @param source            The UUID of the client/member which caused the create/destroy of the proxy.
     * @param proxyManager      The ProxyManager for lazily creating the proxy if not available on the client.
     */
    public LazyDistributedObjectEvent(EventType eventType, String serviceName, String objectName, DistributedObject distributedObject, UUID source, ProxyManager proxyManager) {
        super(eventType, serviceName, objectName, distributedObject, source);
        this.proxyManager = proxyManager;
    }

    @Override
    public DistributedObject getDistributedObject() {
        distributedObject = super.getDistributedObject();
        if (distributedObject == null) {
            distributedObject = proxyManager.getOrCreateProxy(getServiceName(), (String) getObjectName());
        }
        return distributedObject;
    }

}
